#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
void Worker(int cnt)
{
    printf("I am a child, pid:%d, cnt: %d\n", getpid(), cnt );
}
int main()
{ 
  pid_t id = fork();
  if(id == 0)
  {
    int cnt = 10;
    while(cnt)
    {
      Worker(cnt);
      sleep(1);
      cnt --;
    }

    exit(0);
  }
  while(1)
  {
      int status = 0;
      pid_t rid = waitpid(id, &status, WNOHANG );
    if(rid  > 0)
    {
        printf("Child is exited, exit code:%d, exit signal: %d \n" , (status >> 8 )  & 0xff, status & 0x7f);
        break;
    }
    else if(rid == 0)
    {
        printf("child is alive ,wait again, father do other things\n");

    }
    else{
        printf("Wait failed\n");
        break;
        }
    sleep(1);
   }
return 0;
}
